package com.gxa.saas.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.saas.admin.service.ShopService;
import com.gxa.saas.core.common.JsonResult;
import com.gxa.saas.core.entity.BShop;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Api(tags = "商铺管理模块")
@Controller
public class shopController {
    @Autowired
    ShopService shopService;

    @ApiOperation("商铺信息展示接口")
    @ResponseBody
    @GetMapping("/shop/showShop")
    public Map selectShopAll() {

        PageHelper.startPage(0,6);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<BShop> list = shopService.list();
        PageInfo<BShop> pageInfo = new PageInfo<BShop>(list);

        JsonResult jsonResult = new JsonResult();
        if (list.size()!=0) {
            jsonResult.setCode("200");
            jsonResult.setData(list);
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("没有商铺信息");
        }
        return jsonResult.getMap();
    }
    @ApiOperation("搜索店铺接口")
    @ResponseBody
    @GetMapping("/shop/queryByName")
    public Map queryShopByName(@ApiParam(name="sName",value = "这里传店铺名",required = true)@RequestParam("sName") String sName){
        BShop bShop = new BShop();
        bShop.setSName(sName);

        PageHelper.startPage(0,6);//这行是重点，表示从pageNum页开始，每页pageSize条数据
//        编写条件构造器
        LambdaQueryWrapper<BShop> wrapper = Wrappers.lambdaQuery();

            wrapper.like(BShop::getSName,bShop.getSName());
//        调用IService里的方法
        List<BShop> shopList = shopService.list(wrapper);
        PageInfo<BShop> pageInfo = new PageInfo<BShop>(shopList);

        JsonResult jsonResult = new JsonResult();
//        做非空判断
        if (shopList.size()!=0) {
            jsonResult.setCode("200");
            jsonResult.setData(shopList);
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("没有商铺信息");
        }
        return jsonResult.getMap();




    }
    @ApiOperation("已上架商铺展示接口")
    @ResponseBody
    @PostMapping("/shop/statusOn")
    public Map statusOn(){
        BShop bShop = new BShop();

        PageHelper.startPage(0,6);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        QueryWrapper<BShop> wrapper = new QueryWrapper<>();
        wrapper.eq("s_status","1");

        List<BShop> shopList = shopService.list(wrapper);
        PageInfo<BShop> pageInfo = new PageInfo<BShop>(shopList);

        JsonResult jsonResult = new JsonResult();
        if (shopList.size()!=0) {
            jsonResult.setCode("200");
            jsonResult.setData(shopList);
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("没有商铺信息");
        }
        return jsonResult.getMap();

    }
    @ApiOperation("已下架商铺展示接口")
    @ResponseBody
    @PostMapping("/shop/statusOff")
    public Map statusOff(){
        BShop bShop = new BShop();

        PageHelper.startPage(0,6);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        QueryWrapper<BShop> wrapper = new QueryWrapper<>();
        wrapper.eq("s_status","0");

        List<BShop> shopList = shopService.list(wrapper);
        PageInfo<BShop> pageInfo = new PageInfo<BShop>(shopList);

        JsonResult jsonResult = new JsonResult();
        if (shopList.size()!=0) {
            jsonResult.setCode("200");
            jsonResult.setData(shopList);
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("没有商铺信息");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("商铺上下架接口")
    @ResponseBody
    @PostMapping("/shop/status")
    public Map statusShop( BShop bShop){
       BShop bShop1 = new BShop();
       this.shopService.updateById(bShop);
        System.out.println(bShop);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();

    }

}
